package q.test0421library.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpSession;
import q.test0421library.Moudle.UserInfo;
import q.test0421library.Service.UserService;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(String name, String password, HttpSession session){
        // 账号或密码为空
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)) return false;

        UserInfo userInfo = userService.queryuserByName(name);
        if (userInfo == null){
            return false;
        }
        // 账号密码正确
        if(userInfo!=null && password.equals(userInfo.getPassword())){
            // 存储在Session中
            userInfo.setPassword("");
            session.setAttribute("session_user_key",userInfo);
            return true;
        }
        // 账号密码错误
        return false;
    }
}